package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

func main() {
	r := mux.NewRouter()

	// HTTP 路由（仅通过 HTTP 访问）
	r.HandleFunc("/books/{title}/page/{page}", func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		title := vars["title"]
		page := vars["page"]
		fmt.Fprintf(w, "You've requested the book: %s on page %s\n", title, page)
	}).Schemes("http")

	// HTTPS 路由（仅通过 HTTPS 访问）
	r.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Welcome to my Website.")
	}).Schemes("https")

	// 启动服务器
	// 注意：HTTP 和 HTTPS 是不同端口，通常 HTTP:8080, HTTPS:8443
	go func() {
		fmt.Println("HTTP server running on http://localhost:8080")
		log.Fatal(http.ListenAndServe(":8080", r))
	}()

	fmt.Println("HTTPS server running on https://localhost:8443")
	fmt.Println("Use https://localhost:8443/hello to access /hello")
	// 启用https
	log.Fatal(http.ListenAndServeTLS(":8443", "cert.pem", "key.pem", r))
}
